<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\constant\v1\CommonConstant;

/**
 * 系统设置服务类
 **/
class OptionService
{

    /**
     * 获取设置
     *
     * @param string $type
     **/
    public static function getConfig($type = '')
    {
        $config = config('plugin.admin.site');
        if ($type) {
            $config = $config[$type];
        }
//        $config = file_get_contents(base_path('plugin/admin/public/config/site.php'));
//        $name = 'system_config';
//        $config = Util::db()->table('wa_options')
//            ->where('name', $name)
//            ->value('value');
//        $config = json_decode($config, true);
        return $config;
    }

    /**
     * 获取节点到期后保留时间
     **/
    public static function getConfigReserve(){
        $config = self::getConfig('order');
        $order_node_reserve_time = $config['order_node_reserve_time'] ?? 0;
        $hour = $order_node_reserve_time * 3600;
        $time = time();
        $hourAgo = $time - $hour;
        return $hourAgo;
    }

    /**
     * 获取平均时间设置
     **/
    public static function getConfigAvgData()
    {
        $config = self::getConfig('order');
        $order_node_avg = $config['order_node_avg'];
        $data = getLineBreak($order_node_avg);
        $avgData = [];
        foreach ($data as $avg) {
            $arr = explode('|', $avg);
            $arr = getLineEmpty($arr);

            $dayData = explode('-', $arr[0]);
            $amount = $arr[1];
            $avgData[] = [
                'min' => (integer)$dayData[0],
                'max' => (integer)$dayData[1],
                'amount' => $amount,
            ];
        }
        return $avgData;
    }

    /**
     * 获取指定范围内的金额
     **/
    public static function getAvgAmount($data, $day)
    {
        $amount = 0;
        foreach ($data as $avg) {
            if ($avg['min'] <= $day && $day <= $avg['max']) {
                $amount = $avg['amount'];
                break;
            }
        }
        return $amount;
    }

    public static function getConfigOrder()
    {
        $config = self::getConfig('order');
        $order_node_renew_time = $config['order_node_renew_time'] ?? 0;
        $order_node_renew_duration_text = $config['order_node_renew_duration'] ? CommonConstant::get_duration_sec_list()[$config['order_node_renew_duration']] : '';
        $config['text'] = "节点到期前 {$order_node_renew_time} 天，有余额的情况下会自动续费 {$order_node_renew_duration_text} 将到期的节点！";
        return $config;
    }

}
